package com.strategy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.strategy.entity.StrategyEntity;
import com.strategy.mapper.StrategyMapper;
import com.strategy.service.StrategyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 策略服务实现类
 *
 * @author lijun
 * @since 1.0.0
 */
@Service
public class StrategyServiceImpl extends ServiceImpl<StrategyMapper, StrategyEntity> implements StrategyService {

    @Override
    public List<StrategyEntity> getByStrategyKey(String strategyKey) {
        LambdaQueryWrapper<StrategyEntity> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrategyEntity::getStrategyKey, strategyKey);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public StrategyEntity getDefaultByKey(String strategyKey) {
        LambdaQueryWrapper<StrategyEntity> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrategyEntity::getStrategyKey, strategyKey);
        queryWrapper.eq(StrategyEntity::getIsDefault, true);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<StrategyEntity> queryPage(Page<StrategyEntity> page, String key, Integer status) {
        LambdaQueryWrapper<StrategyEntity> queryWrapper  = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(key), StrategyEntity::getStrategyKey, key);
        queryWrapper.eq(status!=null, StrategyEntity::getStatus, status);
        queryWrapper.orderByDesc(StrategyEntity::getId);
        return baseMapper.selectPage(page, queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean setAsDefault(Long id) {
        // 查询当前策略
        StrategyEntity current = getById(id);
        if (current == null) {
            return false;
        }

        // 将同key下其他策略设为非默认
        List<StrategyEntity> others = getByStrategyKey(current.getStrategyKey());
        for (StrategyEntity other : others) {
            if (!other.getId().equals(id) && other.getIsDefault()) {
                other.setIsDefault(false);
                other.setUpdateTime(LocalDateTime.now());
                updateById(other);
            }
        }

        // 将当前策略设为默认
        current.setIsDefault(true);
        current.setUpdateTime(LocalDateTime.now());
        return updateById(current);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void registerStrategy(StrategyEntity strategyEntity) {
        // 如果设置为默认，先将同key下其他默认策略取消
        if (strategyEntity.getIsDefault()) {
            List<StrategyEntity> existStrategies = getByStrategyKey(strategyEntity.getStrategyKey());
            for (StrategyEntity exist : existStrategies) {
                if (exist.getIsDefault()) {
                    exist.setIsDefault(false);
                    updateById(exist);
                }
            }
        }

        // 保存策略
        if (strategyEntity.getId() == null) {
            strategyEntity.setCreateTime(LocalDateTime.now());
            strategyEntity.setUpdateTime(LocalDateTime.now());
            save(strategyEntity);
        } else {
            strategyEntity.setUpdateTime(LocalDateTime.now());
            updateById(strategyEntity);
        }
    }
}